#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef struct QueueNode {
    char user[20];
    struct QueueNode* next;
} QNode;

typedef struct Queue {
    QNode* phead;
    QNode* ptail;
    int size;
} Queue;

void QueueInit(Queue* pq) {
    assert(pq);
    pq->phead = NULL;
    pq->ptail = NULL;
    pq->size = 0;
}

void QueueDestory(Queue* pq) {
    assert(pq);
    QNode* cur = pq->phead;
    while (cur) {
        pq->phead = pq->phead->next;
        free(cur);
        cur = pq->phead;
    }
    pq->phead = pq->ptail = NULL;
    int size = 0;
}
void QueuePop(Queue* pq) {
    assert(pq);
    QNode* del = pq->phead;
    pq->phead = pq->phead->next;
    free(del);
    del = NULL;
    if (pq->phead == NULL) {
        pq->ptail = NULL;
    }
    pq->size--;
}

void QueuePush(Queue* pq, char x[20]) {
    assert(pq);
    QNode* newnode = (QNode*)malloc(sizeof(QNode));
    strcpy(newnode->user, x);
    newnode->next = NULL;
    if (pq->ptail == NULL) {
        pq->phead = pq->ptail = newnode;
    }
    else {
        pq->ptail->next = newnode;
        pq->ptail = newnode;
    }
    pq->size++;
}

int main()
{
    char op[20];
    char name[20];
    char vn[20];
    Queue qv;
    Queue qn;
    QueueInit(&qv);
    QueueInit(&qn);
    int M;
    scanf("%d", &M);
    for (int i = 0; i < M; i++) {
        scanf("%s", op);
        if (strcmp(op, "IN") == 0) {
            scanf("%s%s", name, vn);
            if (strcmp(vn, "V") == 0) {
                QueuePush(&qv, name);
            }
            else {
                QueuePush(&qn, name);
            }
        }
        else {
            scanf("%s", vn);
            if (strcmp(vn, "V") == 0) {
                QueuePop(&qv);
            }
            else {
                QueuePop(&qn);
            }
        }
    }
    QNode* curv = qv.phead;
    QNode* curn = qn.phead;
    while (curv) {
        printf("%s\n", curv->user);
        curv = curv->next;
    }
    while (curn) {
        printf("%s\n", curn->user);
        curn = curn->next;
    }
    QueueDestory(&qv);
    QueueDestory(&qn);
    return 0;
}
